package com.wocai.jrt.orgadmin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;

/***
 * 
 * @author linbaolu
 * @date 2015年7月28日 上午11:56:47
 */
public class MyDateUtils {

	private MyDateUtils() {
	}

	/**
	 * 
	 * 计算两个日期相差的月份数
	 * 
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @param pattern
	 *            日期1和日期2的日期格式
	 * @return 相差的月份数
	 * @throws ParseException
	 */
	public static int countMonths(String date1, String date2, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));

		int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);

		// 开始日期若小月结束日期
		if (year < 0) {
			year = -year;
			return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
		}

		return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
	}

	/**
	 * 计算两个日期相差的天数数
	 * 
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 相差的月份数
	 * @throws ParseException
	 */
	public static int countDays(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个日期相差的小时数数
	 * 
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 相差的小时数Double
	 * @throws ParseException
	 */
	public static Double countHours(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		Double between_hours = (time2 - time1) / (1000 * 3600D);
		return between_hours;
	}

	/**
	 * 获取某年第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearFirst(String year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某年最后一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getYearLast(String year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return new DateTime(currYearLast).plusHours(23).plusMinutes(59).plusSeconds(59).toDate();
	}

	/**
	 * 获取某月第一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getMonthFirst(String year, String month) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某月最后一天日期
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getMonthLast(String year, String month) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		Date currYearLast = calendar.getTime();
		return new DateTime(currYearLast).plusHours(23).plusMinutes(59).plusSeconds(59).toDate();
	}

	/**
	 * 获取某天第一个小时
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getDayFirst(String year, String month, String day) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		calendar.set(Calendar.DATE, Integer.parseInt(day));
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取某天最后一个小时
	 * 
	 * @param year
	 *            年份
	 * @return Date
	 */
	public static Date getDayLast(String year, String month, String day) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, Integer.parseInt(year));
		calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		calendar.set(Calendar.DATE, Integer.parseInt(day));
		Date currYearLast = calendar.getTime();
		return new DateTime(currYearLast).plusHours(23).plusMinutes(59).plusSeconds(59).toDate();
	}

	public static String getYestoday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	public static String getThisMonth() {
		Calendar cal = Calendar.getInstance();
		return new SimpleDateFormat("yyyy-MM").format(cal.getTime());
	}
}
